package com.itheima.controller;

import com.alibaba.fastjson.JSON;
import com.itheima.common.R;
import com.itheima.pojo.Addressbook;
import com.itheima.service.AddressbookService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

/**
 * @author Envy
 */
@RestController
@RequestMapping("/addressBook")
public class AddressbookController {
    @Resource
    private AddressbookService addressbookService;
    @Resource
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 设置默认收货地址
     */
    @PutMapping("/default")
    public R<Object> defaultAddressBook(@RequestBody Addressbook addressbook) {
        Addressbook addressbook1 = addressbookService.defaultAddressBook(addressbook);
        System.out.println("====================================" + addressbook1);
        if (addressbook1 != null) {
            return R.success("设置成功");
        }
        return R.error("失败");
    }

    /**
     * 获取默认收货地址
     */
    @GetMapping("/default")
    public R<Addressbook> selectisDefault() {
        Addressbook addressbook = addressbookService.selectIsDefault();
        return R.success(addressbook);
    }

    /**
     * 获取收货地址列表
     */
    @GetMapping("/list")
    public R<List<Addressbook>> selectAddressBook() {

        String books = redisTemplate.opsForValue().get("addressBooks");
        if (books != null) {
            List<Addressbook> list = JSON.parseArray(books, Addressbook.class);
            return R.success(list);
        }

        List<Addressbook> addressBooks = addressbookService.selectAddressBook();
        redisTemplate.opsForValue().set("addressBooks", JSON.toJSONString(addressBooks));
        if (addressBooks.size() != 0) {
            return R.success(addressBooks);
        }
        return R.error("暂无数据");
    }

    /**
     * 根据id获取收货地址
     */
    @GetMapping("/{id}")
    public R<Addressbook> selectById(@PathVariable String id) {
        Addressbook addressbook = addressbookService.selectById(id);
        if (addressbook != null) {
            return R.success(addressbook);
        }
        return R.error("暂无数据");
    }

    /**
     * 添加收货地址
     */
    @PostMapping()
    public R<Object> addAddressBook(@RequestBody Addressbook addressbook) {
        //获取时间戳,将时间戳作为id
        long timeMillis = System.currentTimeMillis();
        String id = timeMillis + "";
        addressbook.setId(id);
        String id1 = addressbook.getId();
        System.out.println("debug=================================" + id1);

        int i = addressbookService.addAddressBook(addressbook);
        if (i != 0) {
            return R.success("添加成功");
        }
        return R.error("添加失败");
    }

    /**
     * 删除收货地址
     */
    @DeleteMapping()
    public R<Object> deleteAddressBook(@RequestParam("ids") List<String> ids) {
        R r = addressbookService.deleteAddressBook(ids);
        if (r != null) {
            return R.success("删除成功");
        }
        return R.error("删除失败");
    }

    /**
     * 修改收货地址
     */
    @PutMapping()
    public R<Object> updateAddressBook(@RequestBody Addressbook addressbook) {
        int i = addressbookService.updateAddressBook(addressbook);
        if (i != 0) {
            return R.success("修改成功");
        }
        return R.error("修改失败");
    }
}
